#include "collectionitemmodel.h"

#include <QDrag>
#include <QMimeData>

#include <QtDebug>

using namespace Playur;

Playur::CollectionItemModel::CollectionItemModel(QObject* parent)
        : QStandardItemModel(parent)
{

}

#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include "qt_serialize.h"
#include <string>
#include <sstream>

void Playur::CollectionItemModel::addCollection(const QString& xml, const QString& overrideAddress)
{
    Playur::Collection* collection = new Playur::Collection;
    std::stringstream ss(xml.toStdString());
    boost::archive::xml_iarchive archive(ss);

    // wow, this is stupid.
    archive >> BOOST_SERIALIZATION_NVP(collection);

    collections.append(collection);

    foreach(Playur::TrackInfo info, collection->tracks) {
        rawList.append(info);
    }
    
    emit updateProxies();
}

QAbstractItemModel* CollectionItemModel::getListProxy()
{
    ListProxy* proxy = new ListProxy(&rawList);
    connect(this, SIGNAL(updateProxies()), proxy, SLOT(updateProxy()));
    return proxy;
}

Playur::TrackItem::TrackItem(const QString& title, const QString& url)
: QStandardItem(title)
{
this->url = url;
}

void Playur::TrackItem::exec(QWidget* source)
{
    QMimeData* mimeData = new QMimeData;
    mimeData->setText(url);

    QDrag* drag = new QDrag(source);
    drag->setMimeData(mimeData);
    drag->exec(Qt::CopyAction, Qt::CopyAction);
}

void Playur::CollectionItemModel::setFilter(const QString& filter)
{
  QStandardItem* root = invisibleRootItem();
  //root->removeRows();
  //QList<int> toRemove;
  //for(int i=0; i<root->rowCount(); i++)
  //  if(applyFilterForArtist(root, i, filter));
}

Playur::ListProxy::ListProxy(QList<Playur::TrackInfo> *list)
{
    this->list = list;
}

QModelIndex ListProxy::index(int row, int column, const QModelIndex& parent) const
{
    return createIndex(row, column, 0);
}

QModelIndex Playur::ListProxy::parent(const QModelIndex & index) const
{
    return QModelIndex();
}

int Playur::ListProxy::rowCount(const QModelIndex & parent) const
{
    if(parent.parent() != QModelIndex())
        return 0;
    
    return list->count();
}

int Playur::ListProxy::columnCount(const QModelIndex & parent) const
{
    if(parent.parent() != QModelIndex())
        return 0;
    
    return 4;
}

QVariant Playur::ListProxy::data(const QModelIndex & index, int role) const
{
    if(role == Qt::UserRole)
        return qVariantFromValue(((void*)&(list->at(index.row()))));

    if(role != Qt::DisplayRole)
        return QVariant();
    
    if(index.column() == 0)
        return QVariant(list->at(index.row()).title);
    else if(index.column() == 1)
        return QVariant(list->at(index.row()).artist);
    else if(index.column() == 2)
        return QVariant(list->at(index.row()).album);
    
    return QVariant();
}

#include <QtDebug>
void ListProxy::updateProxy()
{
    qDebug() << "called UpdateProxy()";
    //foreach(::Track t, *list)
    //    qDebug() << t.title;
    reset();
}

